Make your own free website on Tripod.com

  Sensors

    Feedback:     (Last updated March 18, 2003 )

Home | Hardware | Sensors | Software | Navigation Algorithms | Downloads | Links

Wheel Encoders

Sharp GP2D02 IR Distance sensor

Wheel Encoder (a.k.a PC Mouse Hack)

In this page you will find useful information that will enable you to use PC mice (PS/2 and Serial) as encoders.

I've compiled the information from several sources and used it not long ago to implement wheel encoders for my bot.

 

There are two mice types out there; Serial and PS/2.

I started with the serial mouse until I zapped the IC, so I had to find an alternative… ;-) Both mice types have many similarities and this brief "tutorial" will include:

o    General preparation

o    Interfacing

        - Serial interface

        - PS/2 interface

 

Preparation steps:

  • For wheel encoders select mice that have mechanical encoders:

  • Two slotted wheels (x and y axis)

  • Each wheel has one IR LED and one dual-phototransistor (see theory of operation to understand how this works).

  • Two to three micro switches which I used as bumper switches, are also tied into the mouse.

  • De-solder the IR LEDs, transistors, and switches.

  • Mount in new location and re-wire all components to original mouse board (see my suggested mounting scheme below):

Wheel encoder and electronics setup

Legend:

(1) motor axis

(2) Photo transistor (the IR LED is mounted on the other side of the disk)

(3) PCB for soldering the opto-electronics and providing wiring solder points

(4) mounting screw or spacer

(5) body plate of Bot

(6) slotted or an alternating printed transparency disk

 

  • Prepare a slotted disk to mount on your wheel shaft.

  • I used an Excel pie chart pasted into Visio and painted black/white. The picture can then printed on a transparency using a laser or inkjet printer.

  • Care should be taken when creating the new slotted wheel. New mice have a very fine alternating slot pattern.

  • For my implementation I used "3-4 year old" mice with approximately 40 slots (1mm slot width) and it works. So scale the picture from above to get the desired encoder size and desired slot width.

  • The mice ICs are special purpose devices, and with the above hack it is not worth the time to reverse engineer them, so just use them as is.

  • These IC's basically collect quadrature pulses and switch closure info, and encode them into data bytes sent over PS/2 or RS232 formats.

Close-up of motor and Wheel Encoder

 

Now for the fun stuff - the interfacing:

 

Starting with the Serial (RS232) mouse interface, which is the easiest to interface with:

  • Hook the mouse up to an RS232 port after making sure you are supplying the mouse with the correct voltage levels.

  • Serial mice need a +/-12v interface, so you can use a MAX232 if your micro does not provide it.

  • Set the baud rate to 1200, 7 data bits, 1 stop bit.

  • Get ready to process the info - the "theory of operation" URL above, lists all the necessary information needed to decode the mouse data packet.

 

The PS/2 interface is a bit trickier – because PS/2 was designed to support many devices (keyboards, touch pads etc.), the devices attached to it requires initialization. If you carefully examines the PS/2 interface, you will notice that once the device is properly configured to send data, it uses a waveform similar to RS-232. The only differences are the higher (and non-standard) bit rate, and the TTL level signal voltage.

NOTE: Although a PS/2 device relies on the synchronization of its CLK signal for the DATA, you can ignore the CLK and reliably use a properly configured RS-232 port to read the incoming bits. Here is how to accomplish that:

Once the device is initialized you can receive the data through a regular RS-232 interface running at 12500 baud, with parity set to ODD and using 8 data bits, and one stop bit.

  • The following interface circuit will assist in properly adjusting the voltage levels required by a PS/ device and and micro-controller:

    • Right hand side is your micro interface: left hand side is the mouse connector.

    • P2.0 through P2.2 are port bits for control (P2.1 = in, P2.2 = out, P2.0 = out)

    • RxD is a regular RS232 input set to: 8 data bits, odd Parity, one stop bit, and 12500 baud.

 

 

Sharp GP2D02 IR Distance sensor

The sensor was built to add distance sensing capabilities to AMR. Distance sensing will enable obstacle avoidance and wall following capabilities. The distance sensor is a Sharp GP2D02 digital distance sensor mounted on a servo head, which points the sensor to different directions. The servo and Sharp sensor were both purchased from Acroname.

Servo control

The servo is controlled via a single control line connected to a I/O port's output pin. The output pin drives the PWM positioning signal to the servo through a 74LS04 inverter (for buffering). The PWM positioning signal has a 20mili-Sec repetition time (~50Hz), and has a 'HI' duration between 0.5mili-Sec and 2.5mili-Sec.
The 'HI' level timing moves the servo through a 180° turn, or ±90° around the center from forward pointing position (or an excellent servo theory of operation refer to http://www.seattlerobotics.org/guide/servos.html at the Seattle Robotics Society web site).

The software controlling the servo I/O port bit is built around a Timer Interrupt. The interrupt routine toggles the output bit, and sets up the timer count for the next one-shot interrupt to occur at the end of the 'HI' (Mark phase) os 'LO' (Space phase) of the PWM signal.
The timer count value in global variable
wPulseWidthCount holds 550 to 2000 value range in the Mark phase (for 0.5 to 2.5mili-Sec pulse width), or 16000 minus the Mark phase count for the Space phase. Thus creating a constant frequency pulse train with variable duty-cycle. Note that count values are CPU and Timer dependant, and may vary!

The complete servo driver code is available on the download page.

Sharp GP2D02 control

The GP2D02 is controlled via two I/O lines, one input and one output. I used the following device timing diagram to code the driver - the complete GP2D02 device spec sheet is available on my download page:

The driver code is written in 'C' and available for download. Note that my implementation is specific to the V25 hardware platform I'm using, but I assume the operating procedure can be easily ported. The principle behind my implementation is described by the following three steps:
a. Trigger the Vin line, and check Vout for low level (as a sanity check)
b. Periodically check Vout and signal end-of-measurement when it goes back to high.
c. Read distance data from Vout by clocking Vin.

The GP2D02 data sheet shows that the distance data coming from the sensor is not linear with distance. Several equations where developed, which linear-ize the measurement read from the sensor (see http://www.ottawarobotics.org/articles/gp2d02/gp2d02.html on the Ottawa Robotics web site).
I chose not to use a linearization function, and implemented a conversion table instead. The conversion table holds 11 value-pairs (distance, data), and is searched for a close-enough data value based on the sensor data byte. After a close-enough value is found, the output distance is calculated by simple interpolation.
Creating a table with the right values is the key, and I finalized my work with the measured values listed in the table below.

 

[cm] Sensor
10 231
15 184
20 154
25 137
30 126
35 118
40 112
50 104
60 99
80 93
100 89

Sensor orientation was modeled after Dirk Stueker's experiments with the Sharp GP2D12, and mounted vertically (IR transmitter above receiver). I used a 3/4" wide L-shaped brace, connected to the servo head,  as a mounting surface for the Sharp GP2D02. The servo turns the sensor through a 180° arc, with its center position pointing the sensor forward.

I finally had some time on my hands, and got to test the GP2D02-and-servo sensor combo. I wrote a simple program that scans the 180° area in from of the robot, and return raw sensor data. The values in the Excel graph below represent the scan position between 500 (full right 90°) and 2000 (full left 90°), and the raw data byte values returned by the GP2D02 sensor.
The graph shows a 10[cm] wide envelop that I placed about 20[cm] in front of the scanning sensor (the "closer" measurements on the left hand side of the graph - right hand side of the bot - are a table leg and my PC).

 

Home ] Parent ]